package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private EditText et_zhanghao;
    private EditText et_mima;
    private Button bt_queren;
    private static final String TAG = "MainActivity";
    private TextView tv_zhuce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        et_zhanghao = findViewById(R.id.et_zh);
        et_mima = findViewById(R.id.et_ma);
        bt_queren = findViewById(R.id.bt_qr);
        tv_zhuce = findViewById(R.id.tv_zc);
    }
    private void initData() {
        String url = "http://10.1.62.39/api/user/login";
        String zhanghaoStr = et_zhanghao.getText().toString().trim();
        String mimaStr = et_mima.getText().toString().trim();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("telephone",zhanghaoStr);
            jsonObject.put("password",mimaStr);
        }catch (JSONException e){
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG,"onResponse"+jsonObject.optString("msg"));
                Toast.makeText(MainActivity.this,jsonObject.optString("msg"),Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"网络请求失败!",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


    private void initEvent() {
    bt_queren.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initData();
            Intent intent=new Intent(MainActivity.this,PingLunActivity.class);
            startActivity(intent);
        }
    });
    tv_zhuce.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent =new Intent(MainActivity.this,ZhuCeActivity.class);
            startActivity(intent);
        }
    });
    }
}